var app = getApp()
var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    shopImages: [],
    shopImagesTemp: [],
  },
  onLoad: function (e) {
    var that = this; 
    console.log(e);
    if(e){
      that.setData({
        id:e.id,
        status:e.status,
        msg:e.msg
      })
    }
    app.setNavtitle("认领");
  },
  newSubmit:function(){
    this.setData({
      status:3
    })
  },
  submitApply: function () {
    var that = this;
    var data = {
      map: 'applet_mobile_claim_apply',
      claimid: that.data.id,
      content: that.data.shopImagesTemp
    }
    console.log(data);
    if (data.content<=0) {
      app.errorTip(that, "请上传材料图片", 2000);
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            status:0,
            msg:res.data.data.msg
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading()
        wx.stopPullDownRefresh();
      }
    });
  },
  // 单图上传
  chooseSingleImage: function (e) {
    var that = this;
    var imgtype = e.currentTarget.dataset.imgtype;
    console.log(imgtype);
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#333",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album', imgtype)
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera', imgtype)
          }
        }
      }
    })
  },
  peiviewImg: function (e) {
    var curimg = e.currentTarget.dataset.curimg;
    var imgs = e.currentTarget.dataset.imgs;
    wx.previewImage({
      current: curimg, // 当前显示图片的http链接
      urls: imgs // 需要预览的图片http链接列表
    })
  },
  //选择单图
  chooseWxImage: function (type, imgtype) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res);
        var temPath = res.tempFilePaths[0];
        that.upload_file(temPath, imgtype);
      }
    })
  },
  // 上传单图到服务器
  upload_file: function (temPath, imgtype) {
    var that = this;
    var memberInfo = that.data.memberInfo;
    wx.showToast({
      title: '上传中',
      icon: 'loading',
      duration: 10000,
      mask: true
    });
    wx.uploadFile({
      url: app.globalData.requestUrl + '?&map=applet_img_upload',
      filePath: temPath,
      name: 'image',
      success: function (res) {
        var data = JSON.parse(res.data);
        var realpath = app.globalData.domin + data.data.path;
        if (data.ec == 200) {
          console.log(realpath);
          var savePath = imgtype + 'Temp';
          that.setData({
            [imgtype]: realpath,
            [savePath]: data.data.path
          })
          console.log(that.data[imgtype]);
          console.log(that.data[savePath]);
        } else {
          wx.showModal({
            title: '提示',
            content: data.em,
            showCancel: false
          });
        }
      },
      complete: function () {
        wx.hideToast();
      }
    });
  },
  //选择多图上传
  chooseMultiImage: function () {
    var that = this;
    var shopImages = that.data.shopImages;
    var shopImagesLength = 0;
    if (shopImages.length < 5) {
      shopImagesLength = 5 - shopImages.length;
    } else {
      shopImagesLength = 0;
    }
    console.log(shopImagesLength);
    app.chooseImageTap(this, shopImagesLength);
  },
  uploadImage: function () {//这里触发图片上传的方法
    var that = this;
    var temPaths = that.data.temPaths;
    var imgArr = that.data.shopImagesTemp;
    var shopImages = that.data.shopImages;
    that.uploadimg({
      url: app.globalData.requestUrl + '&map=applet_img_upload',//这里是你图片上传的接口
      path: temPaths//这里是选取的图片的地址数组
    }, imgArr, shopImages);
  },
  uploadimg: function (data, imgArr, shopImages) {
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.showToast({
      title: '上传中',
      icon: 'loading',
      duration: 10000,
      mask: true
    });
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'image',
      success: (resp) => {
        success++;
        var data = JSON.parse(resp.data);
        var curpath = data.data.path;
        var realpath = app.globalData.domin + data.data.path;
        console.log(shopImages);
        console.log(realpath);
        shopImages.push(realpath);
        imgArr.push(curpath);
      },
      fail: (res) => {
        fail++;
      },
      complete: () => {
        i++;
        if (i == data.path.length) {   //当图片传完时，停止调用        
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
          console.log("dfs////////////");
          console.log(imgArr);
          console.log(shopImages);
          that.setData({
            shopImages: shopImages,
            shopImagesTemp: imgArr
          })
          console.log(that.data.shopImagesTemp);
          console.log("dfs////////////");
          wx.hideToast();
        } else {//若图片还没有传完，则继续调用函数
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data, imgArr, shopImages);
        }
      }
    });
  },
  delImg: function (e) {
    var that = this;
    var delIndex = e.target.dataset.index;
    var shopImages = that.data.shopImages;
    var imgArr = that.data.shopImagesTemp;
    console.log(shopImages);
    console.log(imgArr);
    wx.showModal({
      title: '删除提示',
      content: '确定要删除该图片吗？',
      success: function (res) {
        if (res.confirm) {
          shopImages.splice(delIndex, 1);
          imgArr.splice(delIndex, 1);
          that.setData({
            shopImages: shopImages,
            shopImagesTemp: imgArr
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})